﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using SharePointLoggerArgs;

namespace SharePointLoggerWebParts.UserLogEntryPart
{
    [ToolboxItemAttribute(false)]
    public class UserLogEntryPart : WebPart
    {

        private TextBox Message;
        private Button Go;
        private Label CorrelationId;

        protected override void CreateChildControls()
        {
            Message = new TextBox();
            Message.Text = "Testing SharePoint Logger";
            this.Controls.Add(Message);

            Go = new Button();
            Go.Text = "Go!";
            Go.Click += new EventHandler(Go_Click);
            this.Controls.Add(Go);

            CorrelationId = new Label();
            this.Controls.Add(CorrelationId);
        }

        void Go_Click(object sender, EventArgs e)
        {
            try
            {
                TraceProxyArgs proxyArgs = new TraceProxyArgs();
                proxyArgs.CorrelationID = Guid.NewGuid().ToString();
                proxyArgs.AssemblyName = "SharePointLoggerWebParts.dll";
                proxyArgs.DisplayName = "SharePoint Logger Web Parts";
                proxyArgs.MessageType = TraceProxyArgs.Severity.Information.ToString();
                proxyArgs.Message = Message.Text;


                CorrelationId.Text = SPUtility.ExecuteRegisteredProxyOperation(
                    proxyArgs.TraceProxyOpsAssemblyName,
                    proxyArgs.TraceProxyOpsTypeName,
                    proxyArgs).ToString();
            }
            catch (Exception x)
            {
                CorrelationId.Text = x.Message;
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            Message.RenderControl(writer);
            writer.Write("<br/>");
            Go.RenderControl(writer);
            writer.Write("<br/>");
            CorrelationId.RenderControl(writer);
        }
    }
}
